package com.hnucm.cooklife.model;

import android.database.sqlite.SQLiteDatabase;

import com.blankj.utilcode.util.NetworkUtils;
import com.hnucm.cooklife.base.BaseModel;
//import com.hnucm.cooklife.bean.home.Dish;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.home.DishData;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.contract.recom.Contract;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import io.reactivex.Observable;

public class RecomModel extends BaseModel implements Contract.IRecomModel {
//
//    private int current = 1;
    public RecomModel() {
        setCookies(false);
    }


    @Override
    public Observable<List<Dish>> loadDish(int currentPage) {
//
        Observable<List<Dish>> loadFromLocal = Observable.create(emitter -> {
            List<Dish> dishList = LitePal.findAll(Dish.class);
            emitter.onNext(dishList);
            emitter.onComplete();
        });

        if(NetworkUtils.isConnected()){
            Observable<List<Dish>> loadFromNet = loadFromNet(currentPage);
            return loadFromNet;
        } else {
            return loadFromLocal;
        }


    }
//
    private Observable<List<Dish>> loadFromNet(int currentPage){
        return mApiServer.loadDish(currentPage)
                .map(dish -> {
                    List<Dish> dishList = new ArrayList<>();
                    dish.getData().getRecords().stream().forEach(recordsDTO -> {
                        Dish dishDB = new Dish();
                        dishDB.dishId = recordsDTO.getDishId();
                        dishDB.dishname = recordsDTO.getDishname();
                        dishDB.dishdesc = recordsDTO.getDishdesc();
                        dishDB.dishpic = recordsDTO.getDishpic();
                        dishDB.userid = recordsDTO.getUserid();
                        dishDB.username = recordsDTO.getUsername();
                        dishDB.icon = recordsDTO.getIcon();
                        dishDB.bg = recordsDTO.getBg();
                        dishDB.dishscore = recordsDTO.getDishscore();
                        dishDB.dishdone = recordsDTO.getDishdone();
                        dishDB.collect = recordsDTO.getCollect();
                        dishList.add(dishDB);
                    });
                    if(dishList.size() > 0){
                        LitePal.deleteAll(Dish.class);
                    }
                    LitePal.saveAll(dishList);
                    return dishList;
                });
    }

    @Override
    public Observable<ToAddCollectData> collect(int userId, int dishId) {
        return mApiServer.addCollect(userId, dishId);
    }

    @Override
    public Observable<ToAddCollectData> unCollcet(int userId, int dishId) {
        return mApiServer.unCollect(userId, dishId);
    }

    @Override
    public Observable<List<Dish>> refreshDish(int currentPage) {
        return loadDish(currentPage);
    }
}
